package cn.iocoder.yudao.module.wms.dal.mysql.stock;

import java.math.BigDecimal;
import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.wms.dal.dataobject.stock.StockDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.wms.controller.admin.stock.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * 库存汇总 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface StockMapper extends BaseMapperX<StockDO> {

    default PageResult<StockDO> selectPage(StockPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<StockDO>()
                .eqIfPresent(StockDO::getProductId, reqVO.getProductId())
                .eqIfPresent(StockDO::getStockQty, reqVO.getStockQty())
                .betweenIfPresent(StockDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(StockDO::getId));
    }

    default StockDO selectByProduct(Long productId) {
        return selectOne(StockDO::getProductId, productId);
    }

    @Update("update wms_stock set stock_qty = stock_qty + #{qty} where id = #{id}")
    void handleStockQty(@Param("id") Long id, @Param("qty") BigDecimal qty);
}